<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" label-width="120px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="dataForm.title" placeholder="标题"></el-input>
      </el-form-item>
      <el-form-item label="图片" prop="image">
        <el-upload :action="dataForm.upload" :show-file-list="false"
                   :before-upload="beforeUploadHandle" :on-success="successHandle">
          <el-image v-if="dataForm.image" :src="dataForm.image" class="avatar" style="max-width: 200px"></el-image>
          <el-icon v-else :size="30">
            <Plus/>
          </el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="优惠金额" prop="discount">
        <el-input-number v-model="dataForm.discount" :precision="2" :step="1" :min="0.01" :max="10000"/>
      </el-form-item>
      <el-form-item label="分发类型" prop="distributionType">
        <el-radio-group v-model="dataForm.distributionType">
          <el-radio :label="0">用户领取</el-radio>
          <el-radio :label="1">主动分发</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="优惠券类型" prop="type">
        <el-radio-group v-model="dataForm.type">
          <el-radio :label="0">无门槛</el-radio>
          <el-radio :label="1">满减</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="dataForm.type == 1" label="满减金额" prop="minAmount">
        <el-input-number v-model="dataForm.minAmount" :precision="2" :step="1" :min="0.01" :max="99999999.99"/>
      </el-form-item>
      <el-form-item label="截至时间" prop="deadlineTime">
        <el-date-picker v-model="dataForm.deadlineTime" value-format="YYYY-MM-DD HH:mm:ss"
                        type="datetime" placeholder="截至时间" style="width: 200px;"/>
      </el-form-item>

      <!--      <el-form-item label="开始时间" prop="startTime">
              <el-date-picker v-model="dataForm.startTime" value-format="YYYY-MM-DD HH:mm:ss.000Z"
                              type="datetime" placeholder="开始时间" style="width: 200px;"/>
            </el-form-item>
            <el-form-item label="结束时间" prop="endTime">
              <el-date-picker v-model="dataForm.endTime" value-format="YYYY-MM-DD HH:mm:ss.000Z"
                              type="datetime" placeholder="结束时间" style="width: 200px;"/>
            </el-form-item>-->
      <el-form-item label="优惠券数量" prop="num">
        <el-input v-model="dataForm.num" placeholder="优惠券数量"></el-input>
      </el-form-item>
      <el-form-item label="已领取数量" prop="received">
        <el-input disabled="disabled" v-model="dataForm.received" placeholder="已领取数量"></el-input>
      </el-form-item>
      <el-form-item label="是否结束" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
      </el-form-item>
      <!--      <el-form-item label="内容" prop="content">
        <el-input v-model="dataForm.content" placeholder="内容"></el-input>
      </el-form-item>
      <el-form-item label="链接" prop="url">
        <el-input v-model="dataForm.url" placeholder="链接"></el-input>
      </el-form-item>-->
      <!--      <el-form-item label="排序" prop="rank">
        <el-input v-model="dataForm.rank" placeholder="排序"></el-input>
      </el-form-item>-->
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {reactive, ref} from "vue";
import baseService from "@/service/baseService";
import {ElMessage} from "element-plus";
import {IObject} from "@/types/interface";
import {getToken} from "@/utils/cache";

const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  title: "",
  // content: "",
  // url: "",
  num: "",
  received: "",
  // rank: "",
  image: "",
  status: 0,
  distributionType: 0,
  type: 0,
  minAmount: 0,
  discount: 0,
  deadlineTime: "",
  // startTime: "",
  // endTime: "",
  upload: ""
});

const rules = ref({
  title: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  // content: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  // url: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  num: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  // received: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  rank: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  // image: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  status: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  type: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  distributionType: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  // minAmount: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  discount: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  // startTime: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  // endTime: [{required: true, message: "必填项不能为空", trigger: "blur"}]
  deadlineTime: [{required: true, message: "必填项不能为空", trigger: "blur"}]
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";
  dataForm.upload = `${window.SITE_CONFIG['apiURLUpload']}?token=${getToken()}&type=coupons`

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/yckj/coupons/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 上传之前
const beforeUploadHandle = (file: IObject) => {
  if (file.type !== "image/jpg" && file.type !== "image/jpeg" && file.type !== "image/png" && file.type !== "image/gif") {
    ElMessage.error("只支持jpg、png、gif格式文件！");
    return false;
  }
};

// 上传成功
const successHandle = (res: IObject, file: IObject, list: IObject[]) => {
  dataForm.image = res.data.url;
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/yckj/coupons", dataForm).then((res) => {
      ElMessage.success({
        message: "成功",
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
